#pragma once

#include<BWAPI.h>

#include"ControlerBase.h"

using namespace BWAPI;

class SporeColonyControler : public ControlerBase
{
public:
	SporeColonyControler(Unit* sporeColony)
		:ControlerBase(sporeColony),
		_finishConstruction(false)
	{
	}
	virtual void OnFrame()
	{
		if(IsDead())
		{
			return;
		}
		if(_finishConstruction)
		{
			if(_unitToControl->getGroundWeaponCooldown() <= 0)
			{
				int minHp(9999);
				Unit* bestTarget(NULL);
				for each(Unit* whichTarget in _unitToControl->getUnitsInWeaponRange(WeaponTypes::Subterranean_Tentacle))
				{
					if(Broodwar->self()->isEnemy(whichTarget->getPlayer()))
					{
						int hp = whichTarget->getHitPoints() + whichTarget->getShields();
						if(hp < minHp)
						{
							minHp = hp;
							bestTarget = whichTarget;
						}
					}
				}
				if(bestTarget != NULL)
				{
					_unitToControl->attack(bestTarget);
				}
			}
		}
		else if(_unitToControl->isCompleted() && _unitToControl->getType() == UnitTypes::Zerg_Creep_Colony)
		{
			if(Broodwar->canMake(_unitToControl , UnitTypes::Zerg_Spore_Colony))
			{
				_unitToControl->morph(UnitTypes::Zerg_Spore_Colony);
			}
		}
		else if(_unitToControl->isCompleted() && _unitToControl->getType() == UnitTypes::Zerg_Spore_Colony)
		{
			_finishConstruction = true;
		}
	}
private:
	bool _finishConstruction;
};